package com.example.city_home_app.Model;

import com.example.city_home_app.RetrofitListener;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaseModel {
    private Retrofit retrofit;
    private static final String BASE_URL = "http://siminzhang.top:8088/city_home_server/public/index.php/admin/api/";
    private static final String JavaWeb_BASE_URL =  "http://siminzhang.top:8080/news/";

    public Retrofit getRetrofit() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;
    }

    public Retrofit getRetrofit(int type) {
        retrofit = new Retrofit.Builder()
                .baseUrl(JavaWeb_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;
    }


    public <T> void callenqueue(Call<T> call, final RetrofitListener<T> listener) {
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public static RequestBody toRequestBody(String value) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), value);
        return requestBody;
    }
}
